﻿using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Mvc.Ajax;
using System.Web.Routing;
using System.IO;

namespace System.Web.Mvc.Ajax
{
    /// <summary>
    /// Extension de MVC para crear links a acciones AJAX sobre otros elementos
    /// </summary>
    public static class ActionLinkAjax
    {
        /// <summary>
        /// Construye un AJAX action link que contiene una imagen
        /// </summary>
        /// <param name="helper">Helper AJAX</param>
        /// <param name="imageUrl">Url de la imagen</param>
        /// <param name="altText">Texto alternativo, para ser usado en los atributos alt y title</param>
        /// <param name="actionName">Nombre de la acción</param>
        /// <param name="routeValues">Valores de la ruta</param>
        /// <param name="ajaxOptions">Opciones ajax</param>
        /// <returns></returns>
        public static MvcHtmlString ActionLinkImagen(this AjaxHelper helper, string imageUrl, string altText, string actionName, object routeValues, AjaxOptions ajaxOptions, object atributosHtml = null)
        {
            HtmlHelper Html = new HtmlHelper(helper.ViewContext, helper.ViewDataContainer);
            var img = Html.Image(imageUrl, altText, atributosHtml);
            return ActionLinkExtendido(helper, img, actionName, routeValues, ajaxOptions);
        }

        /// <summary>
        /// Construye un AJAX action link que contiene una imagen
        /// </summary>
        /// <param name="helper">Helper AJAX</param>
        /// <param name="imageUrl">Url de la imagen</param>
        /// <param name="altText">Texto alternativo, para ser usado en los atributos alt y title</param>
        /// <param name="actionName">Nombre de la acción</param>
        /// <param name="controllerName">Nombre del controlador</param>
        /// <param name="routeValues">Valores de la ruta</param>
        /// <param name="ajaxOptions">Opciones ajax</param>
        /// <returns></returns>
        public static MvcHtmlString ActionLinkImagen(this AjaxHelper helper, string imageUrl, string altText, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, object atributosHtml)
        {
            HtmlHelper Html = new HtmlHelper(helper.ViewContext, helper.ViewDataContainer);
            var img = Html.Image(imageUrl, altText, atributosHtml);
            return ActionLinkExtendido(helper, img, actionName, controllerName, routeValues, ajaxOptions);
        }

        /// <summary>
        /// Construye un AJAX action link que contiene un elemento html
        /// </summary>
        /// <param name="helper">Helper AJAX</param>
        /// <param name="tag">Elemento html a contener</param>
        /// <param name="actionName">Nombre de la acción</param>
        /// <param name="routeValues">Valores de la ruta</param>
        /// <param name="ajaxOptions">Opciones ajax</param>
        /// <returns></returns>
        public static MvcHtmlString ActionLinkExtendido(this AjaxHelper helper, MvcHtmlString tag, string actionName, object routeValues, AjaxOptions ajaxOptions)
        {
            MvcHtmlString link = null;
            if(routeValues.GetType().Equals(typeof(RouteValueDictionary)))
                link = helper.ActionLink("[replaceme]", actionName, (RouteValueDictionary)routeValues, ajaxOptions);
            else
                link = helper.ActionLink("[replaceme]", actionName, routeValues, ajaxOptions);
            return MvcHtmlString.Create(link.ToString().Replace("[replaceme]", tag.ToString()));
        }

        /// <summary>
        /// Construye un AJAX action link que contiene un elemento html
        /// </summary>
        /// <param name="helper">Helper AJAX</param>
        /// <param name="tag">Elemento html a contener</param>
        /// <param name="actionName">Nombre de la acción</param>
        /// <param name="controllerName">Nombre del controlador</param>
        /// <param name="routeValues">Valores de la ruta</param>
        /// <param name="ajaxOptions">Opciones ajax</param>
        /// <returns></returns>
        public static MvcHtmlString ActionLinkExtendido(this AjaxHelper helper, MvcHtmlString tag, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions)
        {
            MvcHtmlString link = null;
            if (routeValues.GetType().Equals(typeof(RouteValueDictionary)))
                link = helper.ActionLink("[replaceme]", actionName, controllerName, (RouteValueDictionary)routeValues, ajaxOptions);
            else
                link = helper.ActionLink("[replaceme]", actionName, controllerName, routeValues, ajaxOptions);
            return MvcHtmlString.Create(link.ToString().Replace("[replaceme]", tag.ToString()));
        }
    }
}
